package com.winenine999.admin.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.winenine999.model.Franchisee;
import com.winenine999.model.Page;
import com.winenine999.service.IFranchiseeService;

@Controller
@RequestMapping(value = "/franchisee")
public class FranchiseeController {
	@Autowired
	IFranchiseeService franchiseeService;
	@RequestMapping(value="/in",method=RequestMethod.GET)
	public String index(){
		return "common/main";
	}
	/**
	 * 进入list页面
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(){
	
		return "message/franchisee";	
	}
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length, Object total)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		page=franchiseeService.findPagefranchisees(page);
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	/**
	 * 添加
	 * @param name
	 * @param password
	 * @param createtime
	 * @param realname
	 * @param phone
	 * @param state
	 * @param address
	 * @param alliancename
	 * @param email
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Integer add(String name,String password,String createtime,String realname,String phone,String state,String address,String alliancename,String email){
		
		System.out.println(name);
		System.out.println(password);
		Franchisee franchisee=new Franchisee();
		franchisee.setName(name);
		franchisee.setPassword(password);
		franchisee.setCreatetime(createtime);
		franchisee.setRealname(realname);
		franchisee.setPhone(phone);
		franchisee.setState(state);
		franchisee.setAddress(address);
		franchisee.setAlliancename(alliancename);
		franchisee.setEmail(email);
		
		int num=franchiseeService.insertSelective(franchisee);
		
		return num;
	}
	/**
	 * 修改
	 * @param franchisee
	 * @return
	 */
	@RequestMapping(value = "/updatefranchisee")
	@ResponseBody
	public Integer updatefranchisee(Franchisee franchisee) {
		
		Map<String,Object> map =new HashMap<>();
		
		Integer num=franchiseeService.updateByPrimaryKeySelective(franchisee);
		System.out.println(num);
		return num;
	}
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findbyid")
	@ResponseBody
	public Franchisee findbyid(int id){
		System.out.println(id);
		
		Franchisee franchisee=franchiseeService.selectByPrimaryKey(id);
		
		return franchisee;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deletebyid")
	@ResponseBody
	public Integer deletebyid(Integer id){
		Map<String,Object> map =new HashMap<>();
		Integer num=franchiseeService.deleteByPrimaryKey(id);
		return num;
	}
}
